﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web.Configuration;

namespace OpenLightGroup.Common.ApplicationServices
{

    [ServiceContract(Namespace = "http://openlightgroup.net/ApplicationServices/HealthReportingService/2011/09/")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class HealthReportingService
    {
        private List<HealthReportingProvider> _providers = new List<HealthReportingProvider>();

        public HealthReportingService()
        {
            var s = WebConfigurationManager.GetSection("healthReporting") as HealthReportingConfigurationSection;

            foreach (ProviderSettings item in s.Providers)
            {
                var provider = Activator.CreateInstance(System.Type.GetType(item.Type)) as HealthReportingProvider;

                provider.Initialize(item.Name, item.Parameters);

                _providers.Add(provider);
            }

        }

        [OperationContract]
        public IEnumerable<HealthReportingEntryMetaData> GetEvents(int pageIndex, int pageSize)
        {
            var events = new List<HealthReportingEntryMetaData>();

            foreach (var item in _providers)
            {
                events.AddRange(item.GetEvents(pageIndex, pageSize));
            }

            return events;

        }

        [OperationContract]
        public IEnumerable<HealthReportingEntryMetaData> GetExceptions(int pageIndex, int pageSize)
        {
            var events = new List<HealthReportingEntryMetaData>();

            foreach (var item in _providers)
            {
                events.AddRange(item.GetExceptions(pageIndex, pageSize));
            }

            return events;

        }

        [OperationContract]
        public IEnumerable<HealthReportingEntryMetaData> GetEventsByDate(DateTime startDate, DateTime endDate, int pageIndex, int pageSize)
        {
            var events = new List<HealthReportingEntryMetaData>();

            foreach (var item in _providers)
            {
                events.AddRange(item.GetEventsByDate(startDate, endDate, pageIndex, pageSize));
            }

            return events;
        }

        [OperationContract]
        public IEnumerable<HealthReportingEntryMetaData> GetEventsByType(string eventTypeName, int pageIndex, int pageSize)
        {
            var events = new List<HealthReportingEntryMetaData>();

            foreach (var item in _providers)
            {
                events.AddRange(item.GetEventsByType(eventTypeName, pageIndex, pageSize));
            }

            return events;

        }

        [OperationContract]
        public HealthReportingEntry GetEventDetails(string id)
        {
            HealthReportingEntry output = null;

            if (this._providers != null && this._providers.Count > 0)
            {
                var index = 0;

                while (output == null)
                {
                    output = this._providers[index].GetEventDetails(id);
                    index++;
                }
            }

            return output;

        }

    }

}